﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using EasyStudy.Logic;
using EasyStudy.Properties;
using ComponentFactory.Krypton.Toolkit;

namespace EasyStudy.UI
{
    public partial class FrmShowTodayTasks : KryptonForm
    {
        private DataTable dt;

        private void loadDatasource()
        {
            loadDataSource();
            headerTask.Text = "您今天有" + dt.Rows.Count + "个任务需要更新!";
        }

        public FrmShowTodayTasks()
        {
            InitializeComponent();
            loadDatasource();
            this.dataGridViewTodayTask.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

            for (int i = 0; i < dt.Columns.Count; ++i)
            {
                this.dataGridViewTodayTask.Columns[i].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            }
            this.dataGridViewTodayTask.Columns[0].FillWeight = 10;
            this.dataGridViewTodayTask.Columns[1].FillWeight = 20;
            this.dataGridViewTodayTask.Columns[2].FillWeight = 30;
            this.dataGridViewTodayTask.Columns[3].FillWeight = 20;
            this.dataGridViewTodayTask.Columns[4].FillWeight = 20;

            SetDataGridColor();
            Icon = Resources.task1;
            headerTask.Text = "您今天有" + dt.Rows.Count + "个任务需要更新!";
        }

        public void SetDataGridColor()
        {
            for (int i = 0; i < this.dataGridViewTodayTask.Rows.Count; )
            {
                this.dataGridViewTodayTask.Rows[i].DefaultCellStyle.BackColor = System.Drawing.Color.LightGray;
                i += 2;
            }
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            msgBox.CommonButtons = TaskDialogButtons.OK | TaskDialogButtons.Cancel;

            if (dataGridViewTodayTask.SelectedRows != null &&
                dataGridViewTodayTask.SelectedRows.Count > 0)
            {
                DataGridViewRow dgvRow = dataGridViewTodayTask.SelectedRows[0];
                string subject = dgvRow.Cells[1].Value.ToString();
                string comment = dgvRow.Cells[2].Value.ToString();
                string id = dgvRow.Cells[0].Value.ToString();

                msgBox.Content = "主题：" + subject + "\r\n备注：" + comment;
                msgBox.WindowTitle = "确定要更新此任务吗?";
                DialogResult diaRes = msgBox.ShowDialog(this);
                if (diaRes == System.Windows.Forms.DialogResult.OK)
                {
                    EbbTaskDBMgr.FinishATask(id);
                    loadDatasource();
                }
            }
        }

        private void dataGridViewTodayTask_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right) {
                //若行已是选中状态就不再进行设置
                if (e.RowIndex < 0 || e.RowIndex > dataGridViewTodayTask.Rows.Count - 1)
                {
                    return;
                }

                if (dataGridViewTodayTask.Rows[e.RowIndex].Selected == false)
                {
                    dataGridViewTodayTask.ClearSelection();
                    dataGridViewTodayTask.Rows[e.RowIndex].Selected = true;
                }
                //只选中一行时设置活动单元格
                if (dataGridViewTodayTask.SelectedRows.Count == 1)
                {
                    dataGridViewTodayTask.CurrentCell = dataGridViewTodayTask.Rows[e.RowIndex].Cells[e.ColumnIndex];
                }
                //弹出操作菜单
                contextMenuStripCell.Show(MousePosition.X, MousePosition.Y);
            }
        }

        private void ToolStripMenuItemCopy_Click(object sender, EventArgs e)
        {
            if (dataGridViewTodayTask.SelectedRows[0] == null) {
                return;
            }

            string subject = dataGridViewTodayTask.SelectedRows[0].Cells[1].Value.ToString();
            string comment = dataGridViewTodayTask.SelectedRows[0].Cells[2].Value.ToString();

            StringBuilder sBuilder = new StringBuilder();
            sBuilder.AppendLine("主题:" + subject);
            sBuilder.AppendLine("描述:" + comment);
            Clipboard.SetText(sBuilder.ToString());
            MessageBox.Show("已复制到剪切板\r\n" + sBuilder.ToString());
        }

        private void ToolStripMenuItemEdit_Click(object sender, EventArgs e)
        {
            if (dataGridViewTodayTask.SelectedRows != null && dataGridViewTodayTask.SelectedRows.Count == 1)
            {
                EbbinghausTask task = new EbbinghausTask();

                DataGridViewRow row = dataGridViewTodayTask.SelectedRows[0];
                task.ID = row.Cells[0].Value.ToString();
                task.Type = row.Cells[1].Value.ToString();
                task.Subject = row.Cells[2].Value.ToString();
                task.Comment = row.Cells[3].Value.ToString();

                FrmUpdateEbbTask frmUpdateTask = new FrmUpdateEbbTask(task);
                DialogResult diaRes = frmUpdateTask.ShowDialog(this);
                if (diaRes == System.Windows.Forms.DialogResult.OK)
                {
                    loadDataSource();
                }
            }
        }

        private void loadDataSource()
        {
            dt = EbbTaskDBMgr.GetTodayTasksToDataTable();
            DataView dv = new DataView(dt);
            this.dataGridViewTodayTask.DataSource = dv;
        }

        private void ToolStripMenuItemDelete_Click(object sender, EventArgs e)
        {
            if (dataGridViewTodayTask.SelectedRows != null && dataGridViewTodayTask.SelectedRows.Count == 1)
            {
                DataGridViewRow row = dataGridViewTodayTask.SelectedRows[0];
                string id = row.Cells[0].Value.ToString();

                msgBox.Content = "学习是一项需要长期坚持的事情，您已经确认好了要删除该任务了吗？";
                msgBox.WindowTitle = "删除任务";
                msgBox.CommonButtons = TaskDialogButtons.OK | TaskDialogButtons.Cancel;

                DialogResult dres = msgBox.ShowDialog(this);
                if (dres == System.Windows.Forms.DialogResult.OK)
                {
                    EbbTaskDBMgr.DeleteATask(id);
                    loadDataSource();
                }
            }
        }
    }
}
